package com.wangwenjun.concurrency.chapter14.happenbefore;

import java.util.concurrent.TimeUnit;

public class UseVolatile2 {

    private static Monitor2 monitor = new Monitor2();

    public static void main(String[] args) {

        new Thread(()->{
            while (monitor.flag){

            }
            System.out.println("end");
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        monitor.flag = false;
    }

}

class Monitor2{

    public volatile boolean flag = true;

}
